package com.signway.textwdiget.slice;

import com.signway.textwdiget.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {
    private HiLogLabel logLabel = new HiLogLabel(0, 0, "[MAIN]");
    private Text text = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        HiLog.info(logLabel, "onStart");
        if (findComponentById(ResourceTable.Id_text_helloworld) instanceof Text) {
            text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
            text.setClickedListener(component -> {
                text.startAutoScrolling();
            });
        }
        if (findComponentById(ResourceTable.Id_show_dialog) instanceof Button) {
            Button button = (Button) findComponentById(ResourceTable.Id_show_dialog);
            button.setClickedListener(component -> {
                CommonDialog dialog = new CommonDialog(this);
                dialog.setAutoClosable(true);
                dialog.setCornerRadius(50);
                DirectionalLayout directionalLayout = (DirectionalLayout) LayoutScatter.getInstance(this)
                        .parse(ResourceTable.Layout_dialog_main, null, false);
                dialog.setContentCustomComponent(directionalLayout);
                dialog.show();
            });
        }
        if (findComponentById(ResourceTable.Id_show_toast) instanceof Button) {
            Button button = (Button) findComponentById(ResourceTable.Id_show_toast);
            button.setClickedListener(component -> {
                new ToastDialog(this)
                        .setText("Toast提示框展示")
                        .setAlignment(LayoutAlignment.TOP)
                        .setOffset(0, 100)
                        .setDuration(3000)
                        .show();
            });
        }
        if (findComponentById(ResourceTable.Id_clock) instanceof Clock) {
            Clock clock = (Clock) findComponentById(ResourceTable.Id_clock);
            clock.set24HourModeEnabled(false);
            clock.setFormatIn12HourMode("a HH:mm:ss");
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
